﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NuMvvm.Validation
{
    public class ValidationAttribute : Attribute
    {
        public ValidationAttribute()
        {
            ErrorMessage = string.Format("");
        }

        public ValidationAttribute(string errorMessage)
        {
            ErrorMessage = errorMessage;
        }

        public virtual string ErrorMessage { get; set; }

        protected virtual ValidationResult IsValid(
            object value, 
            ValidationContext validationContext)
        {
            return null;
        }

        public void Validate(
            object value,
            ValidationContext validationContext
        )
        {
            ValidationAttribute validatingAttribute = null;
            var validationResult = IsValid(value, validationContext);
            if (validationResult != null)
            {
                throw new ValidationException(validationResult, validatingAttribute, value);
            }
        }
    }
}
